package com.yc.UI;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;

import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.ContactsDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;
import com.yc.utils.Values;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;

/**
 *联系人面板
 * @author UNO
 * 2021年6月24日下午7:03:14
 */
public class ContactsUI extends Composite {
	private Text text;
	private Table table;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ContactsUI(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		sashForm.setOrientation(SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		text = new Text(composite, SWT.BORDER);
		text.setBounds(206, 26, 178, 26);

		Label label = new Label(composite, SWT.NONE);
		label.setBounds(63, 29, 83, 20);
		label.setText("添加联系人：");

		Button button = new Button(composite, SWT.NONE);
		button.setBounds(510, 24, 98, 30);
		button.setText("添加");

		Composite composite_1 = new Composite(sashForm, SWT.BORDER);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		table = new Table(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblColumn = new TableColumn(table, SWT.CENTER);
		tblColumn.setWidth(205);
		tblColumn.setText("联系人编号");

		TableColumn tblColumn_1 = new TableColumn(table, SWT.CENTER);
		tblColumn_1.setWidth(200);
		tblColumn_1.setText("联系人名字");

		TableColumn tblColumn_2 = new TableColumn(table, SWT.CENTER);
		tblColumn_2.setWidth(200);
		tblColumn_2.setText("联系人身份证");

		TableColumn tblColumn_3 = new TableColumn(table, SWT.CENTER);
		tblColumn_3.setWidth(227);
		tblColumn_3.setText("联系人电话号码");

		TableCursor tableCursor = new TableCursor(table, SWT.NONE);
		tableCursor.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Menu menu = new Menu(tableCursor);
		tableCursor.setMenu(menu);

		MenuItem menuItem = new MenuItem(menu, SWT.NONE);

		menuItem.setText("删除");
		sashForm.setWeights(new int[] {78, 415});


		menu.addMenuListener(new MenuAdapter() {
			@Override
			public void menuShown(MenuEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String status = String.valueOf(ti.getData("t_rname"));
				if(!status.isEmpty()) {
					menuItem.setEnabled(true);
				}
			}
		});
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String tid = ti.getText(1);//获取第一列的值，即商品类型编号
				ContactsDao contactsdao = new ContactsDao();
				int result = contactsdao.delete1( String.valueOf(tid));
				if(result > 0) {
					finds();
				}
			}
		});
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ContactsDao contactsdao = new ContactsDao();
				
				String	account = Values.currentLoginUser.get("u_name").toString();
				String account1 = text.getText().trim();//获取用户写入的名字
				
				if(StringUtil.checkNull(account1)) {
					MessageDialog.openWarning(getShell(), "温馨提示", "联系人的名字不能为空....");
					return;
				}
				int result = contactsdao.add(account, account1);
				if(result <= 0) {
					MessageDialog.openError(getShell(), "失败提示", "联系人添加失败哦....");
					return;
				}else {
					text.setText("");
				}
				List<Map<String, String>> list = contactsdao.finds(account);
				for(Map<String, String> map : list) {
					if(account.equals(map.get("t_rname"))){
						MessageDialog.openError(getShell(), "错误提示", "不能添加用户自身...");
						return;
					}
					if(map.get("u_cardid") == null || map.get("u_cardid").isEmpty()) {  
						contactsdao.delete(account1);
						MessageDialog.openError(getShell(), "错误提示", "此联系人不存在...");
						return;
					}
				}


				finds();
			}
		});
		finds();
	}

	protected void finds() {
		ContactsDao contactsdao = new ContactsDao();
		String	account = Values.currentLoginUser.get("u_name").toString();
		List<Map<String, String>> list = contactsdao.finds(account);//获取联系人名字
		//展示联系人信息
		//清除表单上的所有信息
		table.removeAll();
		if(list == null || list.isEmpty()) {
			return;
		}
		TableItem ti = null;
		for(Map<String, String> map : list) {
			ti = new TableItem(table, SWT.NONE);
			ti.setText(new String[] {map.get("t_id"), map.get("t_rname"), map.get("u_cardid"), map.get("u_tel")});

		}


	}

	@Override
	protected void checkSubclass() {
	}
}
